<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="../js/jquery-1.9.0.js"></script>
<script type="text/javascript">
var webURL = '<%=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()%>'; 
$(function() {
	//alert('<%=request.getParameter("appId")%>');
	var appId = '<%=request.getParameter("appId")%>';
	$.get(webURL + "${middleUrl[0]}/getData", {appId: appId}, function(data) {
		//alert("Data Loaded: " + data);
		//xmlDoc = $.parseXML( data ),// string to xml
	    //$xml = $( xmlDoc ),
	    $xml = $( data ),
	    
	    // 创建表头
	    $head = $xml.find( 'head' );
		var nodes = $head[0].childNodes;
		for(var i=0; i<nodes.length; i++) {
			// IE and FF
			var head = nodes[i].innerHTML == undefined ? nodes[i].text : nodes[i].innerHTML;
			$('<td/>').text(head).appendTo('#head');
		}
		
		functions = $xml.find('functions')[0].childNodes;
		// 创建数据
		content = $xml.find('content')[0].childNodes;
		for(var i=0; i<content.length; i++) {
			var entry = content[i].childNodes;
			for(var j=0; j<entry.length; j++) {
				if(j == 0) {
					$('<tr/>').attr('id', entry[j].innerHTML).appendTo('#user');	
				} else {
					$('<td/>').text(entry[j].innerHTML).appendTo('#'+entry[0].innerHTML);	
					//alert(entry[j].innerHTML);
				}
			}
			// 创建功能按钮
			
			for(var x=0; x<functions.length; x++) {
				//alert(functions[i] + functions[i].getAttribute('name'));
				var name = functions[x].getAttribute('name');
				var url = functions[x].getAttribute('url');
				var visibility = functions[x].getAttribute('visibility');
				var usability = functions[x].getAttribute('usability');
				var type = functions[x].getAttribute('type');
				if(visibility == '1' || usability == '1') {
					/* if(i == 0) {
						$('<a/>').attr('href', webURL + url).text(name).appendTo("#add");
					} else {
						
					} */
					$('<td/>').html($('<input/>').attr('type', 'button').attr('onclick', 'mclick("' + url + '", this, "' + type + '")').attr('value', name)).appendTo('#'+entry[0].innerHTML);
				} else if(usability == '0') {
					$('<td/>').html($('<input/>').attr('type', 'button').attr('onclick', 'mclick("' + url + '", this)').attr('disabled', 'true').attr('value', name)).appendTo('#'+entry[0].innerHTML);
				}
			}
		}
		
		// 使表头列数和实际数据的列数保持一致
		for(var x=0; x<functions.length && nodes.length != 0; x++) {
			var visibility = functions[x].getAttribute('visibility');
			var usability = functions[x].getAttribute('usability');
			if(visibility == '1' || usability == '1' || usability == '0') {
				$('<td/>').appendTo('#head');
			}
		}
		
	});
	
});
	function mclick(url, td, type) {
		
		//alert(1 + url);
		//alert(td);
		var id = $(td).parent().parent().attr('id');
		var functionUrl = webURL + url + "?id=" + id;
		//alert(type);
		if(type == '1' || type == '2') {
			window.open(functionUrl);
		} else if(type == '3') {
			$.ajax({
				   type: "get",
				   url: functionUrl,
				   success: function(msg){
				     alert( "删除: " + msg );
				     window.location.reload();
				   }
				});
		}
	}

</script>
</head>
<body>
<%@include file="../../userinfo.jsp" %>
<div id='add'></div>
<table id="user" border="1px">
	<tr id="head">
	</tr>
</table>
<!-- <input type="button" onclick="mclick()" value="test"/> -->
</body>
</html>